package com.idp.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.app.dto.*;
import com.idp.app.entity.SyAjxxEntity;
import com.idp.app.mapper.SyAjxxMapper;
import com.idp.app.service.SySqhdService;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 商圈划定Controller
 *
 * @author Simon
 */
@Api(tags = "商圈划定")
@RestController
@RequestMapping("/sySqhd/")
public class SySqhdController extends BaseController {

    @Autowired
    private SySqhdService sySqhdService;
    @Autowired
    private SyAjxxMapper syAjxxMapper;

    /**
     * 分页查询
     *
     * @param sySqhdSearchDto
     * @return
     */
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"), @ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")})
    @PostMapping("findByPage")
    public ResultModel<PageModel<SySqhdDto>> findByPage(@RequestBody SySqhdSearchDto sySqhdSearchDto) {
        return ResultModel.success(sySqhdService.findByPage(sySqhdSearchDto));
    }

    /**
     * 通过id查询
     *
     * @param sySqhdDto
     * @return
     */
    @ApiOperation(value = "通过id查询")
    @ApiOperationSupport(includeParameters = {"sySqhdDto.id"})
    @PostMapping("getById")
    public ResultModel<SySqhdDto> getById(@RequestBody SySqhdDto sySqhdDto) {
        return ResultModel.success(sySqhdService.getById(sySqhdDto.getId()));
    }

    /**
     * 通过id查询
     *
     * @param sySqhdDto
     * @return
     */
    @ApiOperation(value = "通过地区编码查询所有商圈-入参：dd")
    @PostMapping("getListByDd")
    public ResultModel<ScreenSySqhdDto> getListByDd(@RequestBody SySqhdDto sySqhdDto) {
        ScreenSySqhdDto dto = new ScreenSySqhdDto();
        List<SqAjslDto> sqAjslDtoList = new ArrayList<>();
        List<SySqhdDto> syAjxxEntityList = syAjxxMapper.selectByLx(sySqhdDto.getLx());
        syAjxxEntityList.stream().forEach(e -> {
            // 商圈编码
            Long id = e.getId();
            LambdaQueryWrapper<SyAjxxEntity> query = new LambdaQueryWrapper<SyAjxxEntity>();
            query.eq(SyAjxxEntity::getSqbm, id);
            query.eq(SyAjxxEntity::getLx, sySqhdDto.getLx());
            Integer count = syAjxxMapper.selectCount(query);
            SqAjslDto sq = new SqAjslDto();
            sq.setSqbm(id + "");
            sq.setCt(count + "");
            sq.setSq(e.getSq());

            List<SqAjlbDto> lists = syAjxxMapper.getSqCount(id, sySqhdDto.getLx());
            sq.setSqAjlbDto(lists);
            sqAjslDtoList.add(sq);
        });
        dto.setSySqhdDtos(syAjxxEntityList);
        dto.setSqAjslDtos(sqAjslDtoList);
        return ResultModel.success(dto);
    }

    /**
     * 新增
     *
     * @param sySqhdDto
     * @return
     */
    @ApiOperation(value = "新增")
    @ApiOperationSupport(ignoreParameters = {"sySqhdDto.id"})
    @PostMapping("add")
    public ResultModel add(@RequestBody SySqhdDto sySqhdDto) {
        sySqhdService.add(sySqhdDto);
        return ResultModel.success("新增成功", null);
    }

    /**
     * 修改
     *
     * @param sySqhdDto
     * @return
     */
    @ApiOperation(value = "修改")
    @PostMapping("update")
    public ResultModel update(@RequestBody SySqhdDto sySqhdDto) {
        sySqhdService.update(sySqhdDto);
        return ResultModel.success("修改成功", null);
    }

    /**
     * 删除
     *
     * @param sySqhdDto
     * @return
     */
    @ApiOperation(value = "删除")
    @ApiOperationSupport(includeParameters = {"sySqhdDto.id"})
    @PostMapping("delete")
    public ResultModel delete(@RequestBody SySqhdDto sySqhdDto) {
        sySqhdService.delete(sySqhdDto.getId());
        return ResultModel.success("删除成功", null);
    }

}
